package com.allways.dubbo.mdm.service.vehicleModel.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.vehicleModel.VehicleModel;
import com.allways.dubbo.mdm.service.vehicleModel.IVehicleModelService;

/**
 * <p>ClassName: IVehicleModelService</p>
 * <p>Description: 车型Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-09-21</p>
 */
@Service("mdmVehicleModelService")
public class VehicleModelServiceImpl extends MdmBaseDaoImpl implements IVehicleModelService {
	Log log = LogFactory.getLog(VehicleModelServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addVehicleModel(VehicleModel vehicleModel) {
   		log.debug("For the new vehicleModel parameters:"+ vehicleModel.toJson());
   		validColumnLen(vehicleModel);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(vehicleModel.getVehicleModelCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getVehicleModelByCode(vehicleModel.getVehicleModelCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("vehicleModel.insertVehicleModel",vehicleModel);
    }

    @Override
    public int updateVehicleModel(VehicleModel vehicleModel) {
    	log.debug("Update for the vehicleModel parameters:"+ vehicleModel.toJson());
    	validColumnLen(vehicleModel);
     	//无code的不能更新
        if(StringUtils.isEmpty(vehicleModel.getVehicleModelCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        VehicleModel oldVehicleModel = getVehicleModelByCode(vehicleModel.getVehicleModelCode());
        if(null == oldVehicleModel){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldVehicleModel.setModelId(vehicleModel.getModelId());
		oldVehicleModel.setModelCode(vehicleModel.getModelCode());
		oldVehicleModel.setModelName(vehicleModel.getModelName());
		oldVehicleModel.setCateId(vehicleModel.getCateId());
		oldVehicleModel.setVehicleLength(vehicleModel.getVehicleLength());
		oldVehicleModel.setVehicleWidth(vehicleModel.getVehicleWidth());
		oldVehicleModel.setVehicleHeight(vehicleModel.getVehicleHeight());
		oldVehicleModel.setVehicleWeight(vehicleModel.getVehicleWeight());
		oldVehicleModel.setWheelbase(vehicleModel.getWheelbase());
		oldVehicleModel.setFromGround(vehicleModel.getFromGround());
		oldVehicleModel.setDisplay(vehicleModel.getDisplay());
		oldVehicleModel.setDataSource(vehicleModel.getDataSource());
		oldVehicleModel.setDeleteMark(vehicleModel.getDeleteMark());
		oldVehicleModel.setCreateUser(vehicleModel.getCreateUser());
		oldVehicleModel.setCreateTime(vehicleModel.getCreateTime());
		oldVehicleModel.setUpdateUser(vehicleModel.getUpdateUser());
		oldVehicleModel.setUpdateTime(vehicleModel.getUpdateTime());
        
        return update("vehicleModel.updateVehicleModel",oldVehicleModel);
    }

    @Override
    public int deleteVehicleModel(VehicleModel vehicleModel) {
    	log.debug("Delete for the vehicleModel parameters:"+ vehicleModel.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(vehicleModel.getVehicleModelCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("vehicleModel.deleteVehicleModel",vehicleModel);
    }

    @Override
    public VehicleModel getVehicleModelByCode(String code) {
    	log.debug("Query for vehicleModel code:" + code);
        return queryForObject("vehicleModel.getVehicleModelByCode",code);
    }
    @Override
    public long getVehicleModelCount(VehicleModel vehicleModel) {
    	log.debug("Query for vehicleModel count parameters:" + vehicleModel.toJson());
        return queryForObject("vehicleModel.getVehicleModelCount",vehicleModel);
    }
   @Override
    public List<VehicleModel> queryVehicleModels(VehicleModel vehicleModel, int firstRow, int lastRow) {
      	log.debug("Query for vehicleModel parameters:" + vehicleModel.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("vehicleModel",vehicleModel);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("vehicleModel.queryVehicleModels", param);
    }
    
    /**
     * 验证字段长度
     * @param vehicleModel 需要验证的对象
     */
    private void validColumnLen(VehicleModel vehicleModel){
		if(!StringUtils.isEmpty(vehicleModel.getModelId()) && vehicleModel.getModelId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getModelCode()) && vehicleModel.getModelCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getModelName()) && vehicleModel.getModelName().length()>80){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{80},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getCateId()) && vehicleModel.getCateId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getVehicleLength()) && vehicleModel.getVehicleLength().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getVehicleWidth()) && vehicleModel.getVehicleWidth().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getVehicleHeight()) && vehicleModel.getVehicleHeight().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getVehicleWeight()) && vehicleModel.getVehicleWeight().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getWheelbase()) && vehicleModel.getWheelbase().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getFromGround()) && vehicleModel.getFromGround().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleModel.getDisplay()) && vehicleModel.getDisplay().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(vehicleModel.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
